package com.my.screenadapter.MPerMission;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import androidx.core.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.my.screenadapter.Base.BaseActivity;
import com.my.screenadapter.PermissionListererInterface;
import com.my.screenadapter.R;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * Created by 张英杰 on 2016/11/15.
 * 运行时权限涉及的几个过程：
 * 第一检查权限是否被授予，使用方法 ContextCompat.checkPermission()；
 * 第二请求获取权限，使用方法 requestPermissions()；
 * 第三用户是否授予应用程序权限，监听回调方法 onRequestPermissionsResult()
 * <p>
 * 郭林视频
 * http://edu.csdn.net/course/detail/3539
 * <p>
 * //Android/lifecircle/files 可以不通过write_stroage权限 重要文件
 * File externalFilesDir = getExternalFilesDir("");//Android/lifecircle/files
 * //Android/lifecircle/Cache 有些软件把这些清理
 */

public class MyPermission extends BaseActivity {
    @BindView(R.id.call)
    Button btncall;
    @BindView(R.id.write)
    Button write;
    @BindView(R.id.btn3)
    Button btn3;

    @Override
    protected void init() {
        btn3.setText("RxPermission_:) 多个请求 一个结果");
        write.setText(write.getText().toString()+" 测试Android/lifecircle/files文件夹");
    }

    @OnClick(R.id.btn3)
    public void getRxPermission() {
        Log.i("OnClick==", "btn3");
        //多个权限 返回一个结果
        RxPermissions mPermission = new RxPermissions(this);
        mPermission
                .request(
//                        granted -> {
//                            if (granted) { // Always true pre-M
//                                // I can control the camera now
//                            } else {
//                                // Oups permission denied
//                            }
//                        }
                        new String[]{Manifest.permission.CALL_PHONE,
                        Manifest.permission.ACCESS_FINE_LOCATION})
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(@NonNull Boolean aBoolean) throws Exception {
                        Log.i("boolean==", aBoolean + "");
                    }
                }
                );
    }

    @OnClick(R.id.write)
    public void sdCard() {
        Util.test();
        //测试 android/lifecircle/files文件夹
        String dd = Environment.getExternalStorageDirectory().toString();
        Log.i("myPrint====1", dd); ///storage/emulated/0
        String absolutePath = getFilesDir().getAbsolutePath();
        Log.i("myPrint====2", absolutePath);///lifecircle/user/0/com.my.screenadapter/files
        Log.i("myPrint====3", getFilesDir().getAbsoluteFile().toString());///lifecircle/user/0/com.my.screenadapter/files
        // TODO: 2017/3/29 可以用 其他的不可用 自动创建MyPrint22 文件夹
        File myPrint = getExternalFilesDir("MyPrint22");
        Log.i("myPrint====4", myPrint.toString());///storage/emulated/0/Android/lifecircle/com.my.screenadapter/files/MyPrint22

        File file2 = new File(getExternalFilesDir("etc") + "/name.txt");
        BufferedWriter writer= null;
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file2)));
            writer.write("111111111222222");
            writer.close() ;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected int initLayoutId() {
        return R.layout.activity_mypermission;
    }

    //Call点击事件
    @OnClick(R.id.call)
    public void testCall(View view) {
        requestRuntimePermission(new String[]{Manifest.permission.CALL_PHONE,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.WRITE_EXTERNAL_STORAGE}, new PermissionListererInterface() {
            @Override
            public void onGranted(List<String> grantedPermission) {
                for (String s : grantedPermission) {
                    Toast.makeText(MyPermission.this, "被允许的权限" + s, Toast.LENGTH_SHORT).show();
                    if (s.equals(Manifest.permission.CALL_PHONE)) {
                        callPhone();
                    }
                }
            }

            @Override
            public void onDenied(List<String> deniedPermission) {
                for (String s : deniedPermission) {
                    Toast.makeText(MyPermission.this, "被  拒绝 的权限" + s, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    //打电话方法
    public void callPhone() {
        Intent intent = new Intent(Intent.ACTION_CALL);
        Uri data = Uri.parse("tel:" + "10086");
        intent.setData(data);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        startActivity(intent);
    }
}
